﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GenericTools;

namespace ReportingManagement
{
    public partial class FacilityRegisteredUsers : Form
    {
        private string facilityCode;
        private int districtId;
        private int regionId;

        public FacilityRegisteredUsers(string facilityCode)
        {
            InitializeComponent();
            this.facilityCode = facilityCode;
            PopulateRegisteredUsers();
        }

        private void PopulateRegisteredUsers()
        {
            DataTable facility = Utilities.GetFacilityDetails(facilityCode);
            if (facility.Rows.Count == 1)
            {
                DataRow row = facility.Rows[0];
                string name = row["FacName"].ToString();
                lblFacilityName.Text = name;
                this.Text = String.Format("Facility Registered Users - {0} {1}", facilityCode, name);

                districtId = Int32.Parse(row["District"].ToString());
                regionId = Int32.Parse(row["Region"].ToString());

                DataTable facilitySMSUsers = Utilities.GetAllSMSUsersByFacility(facilityCode);
                try
                {
                    facilitySMSUsers.Columns.Remove("ApprovedCellNo");
                    facilitySMSUsers.Columns.Remove("LangID");
                    facilitySMSUsers.Columns.Remove("SendSMS");
                    facilitySMSUsers.Columns.Remove("FacCode");
                    facilitySMSUsers.Columns.Remove("Email");
                    facilitySMSUsers.Columns.Remove("FacName");
                    facilitySMSUsers.Columns.Remove("District");
                    facilitySMSUsers.Columns.Remove("RegionName");
                }
                catch { }

                facilitySMSUsers.Columns["CellNum"].ColumnName = "Phone Number";
                facilitySMSUsers.Columns["Name"].ColumnName = "Fullname";
                dgvSMSUsers.DataSource = facilitySMSUsers;

                DataTable dataTable = Utilities.GetUsersByFacility(facilityCode);
                dataTable.Columns.Remove("ID");
                dataTable.Columns.Remove("Username");
                dataTable.Columns.Remove("Password");
                dataTable.Columns.Remove("AccessLevelID");
                dataTable.Columns.Remove("AccessLevelValue");
                dataTable.Columns.Remove("SupportingPartnerId");

                DataTable dt = Utilities.GetUsersByDistrict(districtId);
                foreach (DataRow r in dt.Rows)
                {
                    row = dataTable.NewRow();
                    row["FirstName"] = r["FirstName"];
                    row["LastName"] = r["LastName"];
                    row["PhoneNumber"] = r["PhoneNumber"];
                    row["EmailAddress"] = r["EmailAddress"];
                    dataTable.Rows.Add(row);
                }

                dataTable.Columns["FirstName"].ColumnName = "First Name";
                dataTable.Columns["LastName"].ColumnName = "Last Name";
                dataTable.Columns["PhoneNumber"].ColumnName = "Phone Number";
                dataTable.Columns["EmailAddress"].ColumnName = "E-mail Address";
                dgvSupportingUsers.DataSource = dataTable;
            } 
        }
    }
}
